<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionView::Base</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActionView::Base 
            
                <span class="parent">&lt; 
                    
                    <a href="../Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/actionpack/lib/action_view/base_rb.html">actionpack/lib/action_view/base.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h1 id="label-Action+View+Base">Action View <a href="Base.html">Base</a></h1>

<p>Action View templates can be written in several ways. If the template file
has a <code>.erb</code> extension then it uses a mixture of <a
href="../ERB.html">ERB</a> (included in Ruby) and <a
href="../HTML.html">HTML</a>. If the template file has a
<code>.builder</code> extension then Jim Weirich’s Builder::XmlMarkup
library is used.</p>

<h2 id="label-ERB"><a href="../ERB.html">ERB</a></h2>

<p>You trigger <a href="../ERB.html">ERB</a> by using embeddings such as &lt;%
%&gt;, &lt;% -%&gt;, and &lt;%= %&gt;. The &lt;%= %&gt; tag set is used
when you want output. Consider the following loop for names:</p>

<pre>&lt;b&gt;Names of all the people&lt;/b&gt;
&lt;% @people.each do |person| %&gt;
  Name: &lt;%= person.name %&gt;&lt;br/&gt;
&lt;% end %&gt;</pre>

<p>The loop is setup in regular embedding tags &lt;% %&gt; and the name is
written using the output embedding tag &lt;%= %&gt;. Note that this is not
just a usage suggestion. Regular output functions like print or puts won’t
work with <a href="../ERB.html">ERB</a> templates. So this would be wrong:</p>

<pre>&lt;%# WRONG %&gt;
Hi, Mr. &lt;% puts &quot;Frodo&quot; %&gt;</pre>

<p>If you absolutely must write from within a function use
<code>concat</code>.</p>

<p>&lt;%- and -%&gt; suppress leading and trailing whitespace, including the
trailing newline, and can be used interchangeably with &lt;% and %&gt;.</p>

<h3 id="label-Using+sub+templates">Using sub templates</h3>

<p>Using sub templates allows you to sidestep tedious replication and extract
common display structures in shared templates. The classic example is the
use of a header and footer (even though the Action Pack-way would be to use
Layouts):</p>

<pre>&lt;%= render &quot;shared/header&quot; %&gt;
Something really specific and terrific
&lt;%= render &quot;shared/footer&quot; %&gt;</pre>

<p>As you see, we use the output embeddings for the render methods. The render
call itself will just return a string holding the result of the rendering.
The output embedding writes it to the current template.</p>

<p>But you don’t have to restrict yourself to static includes. Templates can
share variables amongst themselves by using instance variables defined
using the regular embedding tags. Like this:</p>

<pre>&lt;% @page_title = &quot;A Wonderful Hello&quot; %&gt;
&lt;%= render &quot;shared/header&quot; %&gt;</pre>

<p>Now the header can pick up on the <code>@page_title</code> variable and use
it for outputting a title tag:</p>

<pre>&lt;title&gt;&lt;%= @page_title %&gt;&lt;/title&gt;</pre>

<h3 id="label-Passing+local+variables+to+sub+templates">Passing local variables to sub templates</h3>

<p>You can pass local variables to sub templates by using a hash with the
variable names as keys and the objects as values:</p>

<pre>&lt;%= render &quot;shared/header&quot;, { headline: &quot;Welcome&quot;, person: person } %&gt;</pre>

<p>These can now be accessed in <code>shared/header</code> with:</p>

<pre>Headline: &lt;%= headline %&gt;
First name: &lt;%= person.first_name %&gt;</pre>

<p>If you need to find out whether a certain local variable has been assigned
a value in a particular render call, you need to use the following pattern:</p>

<pre>&lt;% if local_assigns.has_key? :headline %&gt;
  Headline: &lt;%= headline %&gt;
&lt;% end %&gt;</pre>

<p>Testing using <code>defined? headline</code> will not work. This is an
implementation restriction.</p>

<h3 id="label-Template+caching"><a href="Template.html">Template</a> caching</h3>

<p>By default, <a href="../Rails.html">Rails</a> will compile each template to
a method in order to render it. When you alter a template, <a
href="../Rails.html">Rails</a> will check the file’s modification time and
recompile it in development mode.</p>

<h2 id="label-Builder">Builder</h2>

<p>Builder templates are a more programmatic alternative to <a
href="../ERB.html">ERB</a>. They are especially useful for generating XML
content. An XmlMarkup object named <code>xml</code> is automatically made
available to templates with a <code>.builder</code> extension.</p>

<p>Here are some basic examples:</p>

<pre class="ruby"><span class="ruby-identifier">xml</span>.<span class="ruby-identifier">em</span>(<span class="ruby-string">&quot;emphasized&quot;</span>)                                 <span class="ruby-comment"># =&gt; &lt;em&gt;emphasized&lt;/em&gt;</span>
<span class="ruby-identifier">xml</span>.<span class="ruby-identifier">em</span> { <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">b</span>(<span class="ruby-string">&quot;emph &amp; bold&quot;</span>) }                      <span class="ruby-comment"># =&gt; &lt;em&gt;&lt;b&gt;emph &amp;amp; bold&lt;/b&gt;&lt;/em&gt;</span>
<span class="ruby-identifier">xml</span>.<span class="ruby-identifier">a</span>(<span class="ruby-string">&quot;A Link&quot;</span>, <span class="ruby-string">&quot;href&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;http://onestepback.org&quot;</span>)  <span class="ruby-comment"># =&gt; &lt;a href=&quot;http://onestepback.org&quot;&gt;A Link&lt;/a&gt;</span>
<span class="ruby-identifier">xml</span>.<span class="ruby-identifier">target</span>(<span class="ruby-string">&quot;name&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;compile&quot;</span>, <span class="ruby-string">&quot;option&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;fast&quot;</span>)  <span class="ruby-comment"># =&gt; &lt;target option=&quot;fast&quot; name=&quot;compile&quot;\&gt;</span>
                                                     <span class="ruby-comment"># NOTE: order of attributes is not specified.</span>
</pre>

<p>Any method with a block will be treated as an XML markup tag with nested
markup in the block. For example, the following:</p>

<pre>xml.div do
  xml.h1(@person.name)
  xml.p(@person.bio)
end</pre>

<p>would produce something like:</p>

<pre>&lt;div&gt;
  &lt;h1&gt;David Heinemeier Hansson&lt;/h1&gt;
  &lt;p&gt;A product of Danish Design during the Winter of '79...&lt;/p&gt;
&lt;/div&gt;</pre>

<p>A full-length RSS example actually used on Basecamp:</p>

<pre class="ruby"><span class="ruby-identifier">xml</span>.<span class="ruby-identifier">rss</span>(<span class="ruby-string">&quot;version&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;2.0&quot;</span>, <span class="ruby-string">&quot;xmlns:dc&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;http://purl.org/dc/elements/1.1/&quot;</span>) <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">channel</span> <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">title</span>(<span class="ruby-ivar">@feed_title</span>)
    <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">link</span>(<span class="ruby-ivar">@url</span>)
    <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">description</span> <span class="ruby-string">&quot;Basecamp: Recent items&quot;</span>
    <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">language</span> <span class="ruby-string">&quot;en-us&quot;</span>
    <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">ttl</span> <span class="ruby-string">&quot;40&quot;</span>

    <span class="ruby-ivar">@recent_items</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">item</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">item</span> <span class="ruby-keyword">do</span>
        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">title</span>(<span class="ruby-identifier">item_title</span>(<span class="ruby-identifier">item</span>))
        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">description</span>(<span class="ruby-identifier">item_description</span>(<span class="ruby-identifier">item</span>)) <span class="ruby-keyword">if</span> <span class="ruby-identifier">item_description</span>(<span class="ruby-identifier">item</span>)
        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">pubDate</span>(<span class="ruby-identifier">item_pubDate</span>(<span class="ruby-identifier">item</span>))
        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">guid</span>(<span class="ruby-ivar">@person</span>.<span class="ruby-identifier">firm</span>.<span class="ruby-identifier">account</span>.<span class="ruby-identifier">url</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@recent_items</span>.<span class="ruby-identifier">url</span>(<span class="ruby-identifier">item</span>))
        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">link</span>(<span class="ruby-ivar">@person</span>.<span class="ruby-identifier">firm</span>.<span class="ruby-identifier">account</span>.<span class="ruby-identifier">url</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@recent_items</span>.<span class="ruby-identifier">url</span>(<span class="ruby-identifier">item</span>))

        <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">tag!</span>(<span class="ruby-string">&quot;dc:creator&quot;</span>, <span class="ruby-identifier">item</span>.<span class="ruby-identifier">author_name</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">item_has_creator?</span>(<span class="ruby-identifier">item</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>More builder documentation can be found at <a
href="http://builder.rubyforge.org.">builder.rubyforge.org.</a></p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Base.html#method-c-cache_template_loading">cache_template_loading</a>,
              </li>
            
              
              <li>
                <a href="Base.html#method-c-cache_template_loading-3D">cache_template_loading=</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  
    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
      
        <li>
          
            <a href="Helpers.html">
              ActionView::Helpers
            </a>
          
        </li>
      
        <li>
          
            <a href="../ERB/Util.html">
              ERB::Util
            </a>
          
        </li>
      
        <li>
          
            <a href="Context.html">
              ActionView::Context
            </a>
          
        </li>
      
    </ul>
  



  

    

    

    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>view_renderer</td>
            <td class='attr-desc'></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-cache_template_loading">
            
              <b>cache_template_loading</b>()
            
            <a href="Base.html#method-c-cache_template_loading" name="method-c-cache_template_loading" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-cache_template_loading_source')" id="l_method-c-cache_template_loading_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f817bb2c485d7363be104fd8a00b28c36a0adefc/actionpack/lib/action_view/base.rb#L158" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-cache_template_loading_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_view/base.rb, line 158</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cache_template_loading</span>
  <span class="ruby-constant">ActionView</span><span class="ruby-operator">::</span><span class="ruby-constant">Resolver</span>.<span class="ruby-identifier">caching?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-cache_template_loading-3D">
            
              <b>cache_template_loading=</b>(value)
            
            <a href="Base.html#method-c-cache_template_loading-3D" name="method-c-cache_template_loading-3D" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-cache_template_loading-3D_source')" id="l_method-c-cache_template_loading-3D_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f817bb2c485d7363be104fd8a00b28c36a0adefc/actionpack/lib/action_view/base.rb#L162" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-cache_template_loading-3D_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_view/base.rb, line 162</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cache_template_loading=</span>(<span class="ruby-identifier">value</span>)
  <span class="ruby-constant">ActionView</span><span class="ruby-operator">::</span><span class="ruby-constant">Resolver</span>.<span class="ruby-identifier">caching</span> = <span class="ruby-identifier">value</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                        </div>

    </div>
  </body>
</html>    